<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getOrdersList()"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单数据列表 -->
      <el-table stripe border :data="ordersList" size="medium">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number" width="500px"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="80px"></el-table-column>
        <el-table-column label="是否付款" prop="pay_status" width="80px">
          <template slot-scope="scope">
            <el-tag type="warning" v-if="scope.row.pay_status === '0'">未付款</el-tag>
            <el-tag type="success" v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="80px"></el-table-column>
        <el-table-column label="下单时间" prop="update_time" width="160px">
          <template slot-scope="scope">
            {{ scope.row.update_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip content="修改收货地址" placement="bottom" effect="light">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="modifSite(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="物流进度" placement="bottom" effect="light">
              <el-button type="success" icon="el-icon-location-outline" size="mini" @click="logistics(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" :visible.sync="modifDialogVisible" width="50%" @close="modifOut()">
      <el-form :model="modifSiteForm" :rules="modifSiteRules" ref="modifSiteRef" label-width="100px">
        <el-form-item label="省市区/县" prop="cityname">
          <el-cascader v-model="modifSiteForm.cityname" :options="citydata" :props="{ expandTrigger: 'hover' }"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="consignee_addr">
          <el-input v-model="modifSiteForm.consignee_addr"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modifDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 展示物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="logisticsDialogVisible" width="50%">
      <el-timeline reverse>
        <el-timeline-item v-for="(item, index) in logisticsList" :key="index" :timestamp="item.ftime">
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
// 导入省市区级联组件
import citydata from './citydata'
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        // 当前页数
        pagenum: 1,
        // 显示的页数
        pagesize: 5
      },
      total: 0,
      //   订单数据列表
      ordersList: [],
      //   修改地址对话框隐藏与显示
      modifDialogVisible: false,
      //   修改地址表单数据
      modifSiteForm: {
        cityname: [],
        consignee_addr: ''
      },
      modifSiteRules: {
        cityname: [{ required: true, message: '请输入省市区/县', trigger: 'blur' }],
        consignee_addr: [{ required: true, message: '请输入详细地址', trigger: 'blur' }]
      },
      //   省市区插件
      citydata,
      //   物流进度对话框显示与隐藏
      logisticsDialogVisible: false,
      //   物流进度数据列表
      logisticsList: []
    }
  },
  //   钩子函数
  created() {
    this.getOrdersList()
  },
  methods: {
    async getOrdersList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      //   获取成功后渲染到页面
      this.ordersList = res.data.goods
      this.total = res.data.total
    },
    // 当页数发生改变时触发的函数
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize
      this.getOrdersList()
    },
    // 当前显示的页码发生改变时触发函数
    handleCurrentChange(newnum) {
      this.queryInfo.pagenum = newnum
      this.getOrdersList()
    },
    // 修改收货地址 {
    modifSite(uid) {
      this.modifDialogVisible = true
    },
    // 重置表单
    modifOut() {
      this.$refs.modifSiteRef.resetFields()
    },
    // 展示物流进度
    async logistics() {
      // 发送请求获取物流进度数据
      const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      //   成功后渲染到页面
      this.logisticsList = res.data
      this.logisticsDialogVisible = true
    }
  }
}
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
